package com.hualan.sfzy.service.counselor.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.counselor.Counselor;
import com.hualan.sfzy.entity.counselor.CounselorGrade;
import com.hualan.sfzy.mapper.counselor.CounselorGradeMapper;
import com.hualan.sfzy.mapper.counselor.CounselorMapper;
import com.hualan.sfzy.service.counselor.CounselorGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("CounselorGradeService")
public class CounselorGradeServiceImpl implements CounselorGradeService {
    @Autowired
    private CounselorGradeMapper counselorGradeMapper;
    @Autowired
    private CounselorMapper counselorMapper;

    @Override
    public int add(CounselorGrade counselorGrade) {
        return counselorGradeMapper.insert(counselorGrade);
    }

    @Override
    @Transactional
    public int remove(Integer id) {
        CounselorGrade counselorGrade = counselorGradeMapper.selectById(id);
        Counselor counselor = new Counselor();
        Integer gradeId = counselorGrade.getId();
        counselor.setCounselorGradeId(gradeId);
        List<Counselor> list = counselorMapper.select(counselor);
        if (list.size() > 0) {
            throw new RuntimeException("咨询师中存在该等级");
        }

        return counselorGradeMapper.delete(id);
    }

    @Override
    @Transactional
    public int removeBatch(Integer[] ids) {
        for (Integer id : ids) {
            CounselorGrade counselorGrade = counselorGradeMapper.selectById(id);
            Counselor counselor = new Counselor();
            Integer gradeId = counselorGrade.getId();
            counselor.setCounselorGradeId(gradeId);
            List<Counselor> list = counselorMapper.select(counselor);
            if (list.size() > 0) {
                throw new RuntimeException("咨询师中存在该等级");
            }
        }
        return counselorGradeMapper.deleteBatch(ids);
    }

    @Override
    public int edit(CounselorGrade counselorGrade) {
        return counselorGradeMapper.update(counselorGrade);
    }

    @Override
    public List<CounselorGrade> find() {
        return counselorGradeMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, CounselorGrade entity) {
        PageHelper.startPage(pageNum, pageSize);

        List<CounselorGrade> list = counselorGradeMapper.select(entity);

        PageInfo<CounselorGrade> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();

        map.put(CommonConstant.PAGE_LIST,list);
        map.put(CommonConstant.PAGE_TOTAL,pageInfo.getPages());
        map.put(CommonConstant.TOTAL,pageInfo.getTotal());

        return map;
    }

    @Override
    public CounselorGrade findById(Integer id) {
        return counselorGradeMapper.selectById(id);
    }

}
